﻿Public Class CropImage
    Inherits CoreClass

    Dim wImg As Integer = 0
    Dim hImg As Integer = 0
    Dim id As Integer = 0
    Dim type As Integer = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            wImg = Integer.Parse(Request.QueryString("w"))
            hImg = Integer.Parse(Request.QueryString("h"))
            id = Integer.Parse(Request.QueryString("id"))
            type = Integer.Parse(Request.QueryString("type"))
            If Not Page.IsPostBack Then
                loadImg()
            End If
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Các tham số không hợp lệ.")
            End If
            Exit Sub
        End Try
    End Sub

    Public Sub loadImg()
        Try
            'wImg = Integer.Parse(Request.QueryString("w"))
            'hImg = Integer.Parse(Request.QueryString("h"))
            'id = Integer.Parse(Request.QueryString("id"))
            'type = Integer.Parse(Request.QueryString("type"))
            sql = "spi_getImageByID "
            sql &= objFunction.ValueCheck(id) & ", "
            sql &= objFunction.ValueCheck(type)
            objData.ExeReader(sql)
            If objData.dataRead.Read Then
                Dim d1 As Date = Date.Parse(objData.dataRead("Date").ToString())
                Dim strSaveFolder As String = "/EditorData/image" & Format(d1, "/yyyy/MM/dd/")
                Dim strDateFolder As String = Format(d1, "\\yyyy\\MM\\dd\\")
                
                imgMain.ImageUrl = strSaveFolder & objData.dataRead("Image").ToString()
                imgThumb.ImageUrl = strSaveFolder & objData.dataRead("Image").ToString()
                lblSaveFolder.Text = Server.MapPath("/") & "EditorData\image" & strDateFolder
                lblImg.Text = objData.dataRead("Image").ToString()
                lblImg2.Text = objData.dataRead("Image2").ToString()
            Else
                objAlert.ErrorBox(Page, "Bản ghi không tồn tại trong hệ thống, hãy kiểm tra lại")
            End If
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub

    ' Xử lý sự kiện Crop ảnh
    Private Sub btnCrop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCrop.Click
        Try
            ' Get param
            Dim x1, y1, wSelect, hSelect As Integer
            x1 = Convert.ToInt32(Int(Double.Parse(txtX1.Text)))
            y1 = Convert.ToInt32(Int(Double.Parse(txtY1.Text)))
            wSelect = Convert.ToInt32(Int(Double.Parse(txtWidthSelect.Text)))
            hSelect = Convert.ToInt32(Int(Double.Parse(txtHeightSelect.Text)))

            Dim strSaveFolder As String = lblSaveFolder.Text
            Dim img As String = lblImg.Text
            Dim img2 As String = lblImg2.Text
            ' Xoa anh thumbnail cua neu co
            Dim src As String = strSaveFolder & img2
            If System.IO.File.Exists(src) Then
                System.IO.File.Delete(src)
            End If
            ' Thumbnail Image
            Dim objThumb As New Thumbnail.Thumbnail
            objThumb.InputFile = strSaveFolder & img
            objThumb.Scale = True  'tu dong thi = false, bang tay thi TRUE
            objThumb.Width = wSelect
            objThumb.Height = hSelect
            objThumb.CropWidth = wImg
            objThumb.CropHeight = hImg
            objThumb.X1 = x1 'bo di neu k crop tu dong
            objThumb.Y1 = y1 'bo di neu k crop tu dong
            objThumb.CropType = Thumbnail.CropType.MiddleCenter
            Dim thumbName As String = objThumb.CreateImageByCoordinates() 'call ham crop bang tay
            ' Update anh moi vao database
            sql = "spi_updateImageThumbnail "
            sql &= objFunction.ValueCheck(id) & ", "
            sql &= objFunction.ValueCheck(type) & ", "
            sql &= objFunction.FieldCheck(thumbName)
            objData.ExeNonQuery(sql)
            ' Redirect ve trang quan ly
            If type = 0 Then
                objAlert.InfoBox(Page, "Update ảnh thành công.", "Web/Product/ProductManager.aspx")
            Else
                objAlert.InfoBox(Page, "Update ảnh thành công.", "Web/New/NewManager.aspx")
            End If
        Catch ex As Exception
            If objConfig.Developer.Equals("1") Then
                objAlert.ErrorBox(Page, "File: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.DeclaringType.Name & _
                                  " --- Function: " & (New System.Diagnostics.StackTrace).GetFrame(0).GetMethod.Name & _
                                  " --- Mess: " & ex.Message.Replace("'", """"))
            Else
                objAlert.ErrorBox(Page, "Lỗi kỹ thuật, hãy thông báo lại với Admin, xin cảm ơn.")
            End If
        End Try
    End Sub
End Class